﻿Imports MICROFORMA.BEL
Imports MICROFORMA.BLL

Public Class frmAddLotes

    Dim _LoteBE As New LoteBE
    Dim _LoteBL As New LoteBL

    Private _DetalleLote As List(Of DetalleLoteBE)
    Public Property DetalleLoteBE() As List(Of DetalleLoteBE)
        Get
            Return _DetalleLote
        End Get
        Set(ByVal value As List(Of DetalleLoteBE))
            _DetalleLote = value
        End Set
    End Property

    Private _bolUpdate As Boolean
    Public Property bolUpdate() As Boolean
        Get
            Return _bolUpdate
        End Get
        Set(ByVal value As Boolean)
            _bolUpdate = value
        End Set
    End Property

    Private _IdSolicitud As Integer
    Public Property pIdSolicitud() As Integer
        Get
            Return _IdSolicitud
        End Get
        Set(ByVal value As Integer)
            _IdSolicitud = value
        End Set
    End Property


    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub

    Private Sub btnBuscarCliente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscarCliente.Click
        Dim _frmListSolicitud As New frmListSolicitud
        _frmListSolicitud.ShowDialog()
        pIdSolicitud = _frmListSolicitud.pIdSolicitud
        txtSolicitud.Text = _frmListSolicitud.pNombre
        _frmListSolicitud.Dispose()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregarDetalle.Click
        Dim _frmAddDetalleLote As New frmAddDetalleLote
        _frmAddDetalleLote.ListDetalle = DetalleLoteBE
        _frmAddDetalleLote.ShowDialog()
        DetalleLoteBE = _frmAddDetalleLote.ListDetalle
        Call LoadList()
        _frmAddDetalleLote.Dispose()

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

    Private Sub LoadList()
        lstvIndices.Items.Clear()
        Dim lviItem As ListViewItem
      
        For i As Integer = 0 To DetalleLoteBE.Count - 1
            lviItem = New ListViewItem
            lviItem.Text = 1 + i
            lviItem.SubItems.Add(DetalleLoteBE(i).NombreTipoDocumento)
            lviItem.SubItems.Add(DetalleLoteBE(i).NombreTipoContenedor)
            lviItem.SubItems.Add(DetalleLoteBE(i).CantidadDocumentos)
            lviItem.SubItems.Add(DetalleLoteBE(i).CantidadFolios)
            lviItem.SubItems.Add(DetalleLoteBE(i).Comentarios)
            lstvIndices.Items.Add(lviItem)
        Next

    End Sub


    Private Sub frmAddLotes_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        DetalleLoteBE = New List(Of DetalleLoteBE)
        LoadList()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuitarDetalle.Click
        Dim i As Integer = 0
        i = lstvIndices.SelectedItems(0).Text
        i = i - 1
        If MessageMakeAction("Esta Seguro de eliminar el Registro Seleccionado.") Then
            DetalleLoteBE.RemoveAt(i)
            Call LoadList()
        End If
    End Sub

    Private Sub btnAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
        If ValidateForm() Then
            _LoteBE = setEntity()
            If Not bolUpdate Then
                If _LoteBL.Add(_LoteBE) = "OK" Then
                    Call MessageMistake("La operación se llevo a cabo con exito.", True)
                    Me.Close()
                Else
                    Call MessageMistake("Ha ocurrido problemas al llevarse a cabo la operacion. ")
                End If
            Else
                If _LoteBL.Edit(_LoteBE) = "OK" Then
                    Call MessageMistake("La operación se llevo a cabo con exito.", True)
                    Me.Close()
                Else
                    Call MessageMistake("Ha ocurrido problemas al llevarse a cabo la operacion. ")
                End If
            End If

        End If

    End Sub

    Private Function ValidateForm() As Boolean
        'Pestaña General
        If Trim(txtNombreLote.Text) = "" Then
            Call MessageMistake("Por Favor ingrese el nombre del lote.")
            txtNombreLote.Focus()
            Exit Function
        End If
        If pIdSolicitud = 0 Then
            Call MessageMistake("Por favor selecione una solicitud.")
            Exit Function
        End If


        If DetalleLoteBE.Count = 0 Then
            Call MessageMistake("Por favor ingrese un detalle.")
            Exit Function
        End If

        ValidateForm = True

    End Function

    Private Function setEntity() As LoteBE
        Dim _LoteBE As New LoteBE
        _LoteBE.IdSolicitud = pIdSolicitud
        _LoteBE.Nombre = txtNombreLote.Text
        _LoteBE.DetalleLoteBE = DetalleLoteBE
        Return _LoteBE

    End Function

End Class